#!/usr/bin/env python

# Copyright 2012 JapakGIS Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import models

from google.appengine.ext import db


def PutLocations(eds, ltype):#put the wards and the counties into the db
    edList = []
    for ed in eds: #loop through the results array getting each entity and storing it to the db
        if ltype == "county":
            edList.append(models.County(key_name="%s %s"%(ed["name"], ltype), name=ed["name"]))
        elif ltype == "constituency":
            edList.append(models.Constituency(key_name="%s %s"%(ed["name"], ltype), name=ed["name"], county=ed["parent"]))
        elif ltype =="ward":
            edList.append(models.Ward(key_name="%s %s"%(ed["name"], ed["parent"]), name=ed["name"], constituency=ed["parent"]))

    db.put(edList)   
    
def PutPS(ps, start, stop):
    psList = []

    for p in ps:
        pollingstation = models.PollingStations(key_name="%s %s %s %s"%(p['name'],p['county'],p['constituency'],p['ward']))
        pollingstation.name = p['name']
        pollingstation.county = p["county"]
        pollingstation.constituency = p["constituency"]
        pollingstation.ward = p["ward"]
        pollingstation.point =  "%s,%s"%(str(p["latitude"]), str(p["longitude"]))
        psList.append(pollingstation)

    db.put(psList[start:stop])

        
